import os
from config import ini_op
import requests

# 本地文件路径
file_path = r'D:\code\app_report_back20241201\backend\file\202401051507科目字典导出.xlsx'

def file_uploads(file_path):
    app_port = ini_op.getinivalue('app_config', 'app_port')[0]
    # 上传文件的 URL
    url = f'http://localhost:{app_port}/file_upload/upload?lx=zd'  # 根据实际情况修改端口号

    # 检查文件是否存在
    if not os.path.exists(file_path):
        print(f"文件 {file_path} 不存在")
        exit(1)

    # 打开文件并准备上传
    with open(file_path, 'rb') as file:
        files = {'file': (os.path.basename(file_path), file, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')}
        
        try:
            response = requests.post(url, files=files)
            response.raise_for_status()  # 如果响应状态码不是 200，抛出异常
            print("上传成功:", response.json())
        except requests.exceptions.HTTPError as http_err:
            print(f"HTTP 错误: {http_err}")
        except Exception as err:
            print(f"其他错误: {err}")

if __name__ == '__main__':
    file_uploads(file_path)